/**
 * 
 */
package labox.innovation.gameserver.model.itemcontainer;

import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.ItemTable;
import labox.innovation.gameserver.model.ConsignmentItem;
import labox.innovation.gameserver.model.ConsignmentManager;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.L2ItemInstance.ItemLocation;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.templates.item.L2Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *@author ljsh
 *@description 寄售仓库
 */
public class ConsignmentWarehouse {

	protected static final Logger _log = LoggerFactory.getLogger(ConsignmentWarehouse.class.getName());

	/**
	 * 仓库物品数量上限
	 */
	private static final int COUNTLIMIT = 5;

	private static final int RECORDLIMIT = 5;

	/**
	 * 仓库所有者ID
	 */
	private long _owner;

	/**
	 * 寄售物品列表
	 */
	private LinkedList<ConsignmentItem> itemList;

	/**
	 * 买卖记录
	 */
	private LinkedList<SalesRecord> buySellRecords = new LinkedList<SalesRecord>();

	/**
	 * 是否在数据库有该记录
	 */
	private boolean isInDb = false;

	private AtomicInteger _moneySliver = new AtomicInteger(0); //银子

	private AtomicInteger _moneyNote = new AtomicInteger(0); //银票

	private AtomicInteger _moneyYuanBao = new AtomicInteger(0); //元宝

	private ConsignmentManager manager;

	/**
	 * 
	 */
	public ConsignmentWarehouse(FuzePcInstance owner) {
		this(owner.getObjectId());
	}

	public ConsignmentWarehouse(long owner) {
		_owner = owner;
		itemList = new LinkedList<ConsignmentItem>();
		ConsignmentManager.addConsignmentWarehouse(this);
	}

	public boolean isIndb() {
		return isInDb;
	}

	public void SetIsIndb(boolean isInDb) {
		this.isInDb = isInDb;
	}

	/**
	 * @return 寄售仓库是否已满
	 */
	public boolean isFull() {
		if (itemList == null) {
			return false;
		} else {
			return itemList.size() >= COUNTLIMIT;
		}
	}

	public long getOwnerId() {
		return _owner;
	}

	/**
	 * @return 返回仓库物品的迭代器
	 */
	public Iterator<ConsignmentItem> iterator() {
		return itemList == null ? null : itemList.iterator();
	}

	/**
	 * @param item
	 * @return 添加寄售物品
	 */
	public boolean addConsignmentItem(ConsignmentItem item) {
		boolean rs = true;
		if (isFull() || item == null)
			return false;
		if (itemList.add(item)) {
			item.getItem().setLocation(ItemLocation.CONSIGNMENT);

		} else {
			rs = false;
		}
		return rs;
	}

	public L2ItemInstance createConsignmentItem(String process, int itemId, int count, FuzePcInstance actor) {

		L2ItemInstance item = null;
		for (int i = 0; i < count; i++) {
			L2Item template = ItemTable.getInstance().getTemplate(itemId);
			if (template == null) {
				_log.error((actor != null ? "[" + actor.getName() + "] " : "") + "Invalid ItemId requested: " + itemId, Config.SERVER_ID);
			}
			item = ItemTable.getInstance().createItem(process, itemId, template.isStackable() ? count : 1, actor, null);
			item.setOwnerId(_owner);
			if (template.isStackable() || !Config.MULTIPLE_ITEM_DROP)
				break;
		}
		return item;
	}

	/**
	 * @param item
	 * @return 移除寄售物品
	 */
	public boolean removeConsignmentItem(ConsignmentItem item) {
		if (item == null)
			return false;
		return itemList.remove(item);
	}

	/**
	 * @return 上架物品数量
	 * 
	 */
	public int getShelveItemCount() {
		int count = 0;
		Iterator<ConsignmentItem> it = iterator();
		while (it.hasNext()) {
			ConsignmentItem item = it.next();
			if (item.isShelve()) {
				count++;
			}
		}
		return count;
	}

	public LinkedList<ConsignmentItem> getItemList() {
		return itemList;
	}

	public void setItemList(LinkedList<ConsignmentItem> itemList) {
		this.itemList = itemList;
	}

	public LinkedList<SalesRecord> getBuySellRecords() {
		return buySellRecords;
	}

	public void setBuySellRecords(LinkedList<SalesRecord> buySellRecords) {
		this.buySellRecords = buySellRecords;
	}

	/**
	 * @param objectId
	 * @return 获取指定id物品
	 */
	public ConsignmentItem getConsignmentItem(long objectId) {
		Iterator<ConsignmentItem> it = iterator();
		ConsignmentItem item = null;
		while (it.hasNext()) {
			item = it.next();
			if (item.getItemId() == objectId)
				break;
		}
		return item;
	}

	public void addMoney(int type, int count) {
		if (type == PcInventory.MONEY_SLIVER_ID) {
			_moneySliver.getAndAdd(count);
		}
		if (type == PcInventory.MONEY_NOTE_ID) {
			_moneyNote.getAndAdd(count);
		}
		if (type == PcInventory.MONEY_YAUNBAO_ID) {
			_moneyYuanBao.getAndAdd(count);
		}
	}

	public void reTakeMoney(FuzePcInstance player) {
		int yuanbao = _moneyYuanBao.get();
		int yinpiao = _moneyNote.get();
		SystemMessage msg = null;
		if (yuanbao > 0) {
			player.addMoneyYuanBao("取回寄售仓库元宝", yuanbao, null, true);
			_moneyYuanBao.getAndAdd(-yuanbao);
			msg = new SystemMessage(SystemMessageId.BY_THE_CONSGNMENT_YOU_GET_YUANBAO);
			msg.addNumber(yuanbao);
			player.sendPacket(msg);
		}
		if (yinpiao > 0) {
			player.addMoneyNote("取回寄售仓库银票", yinpiao, null, true);
			_moneyNote.getAndAdd(-yinpiao);
			msg = new SystemMessage(SystemMessageId.BY_THE_CONSGNMENT_YOU_GET_NOTE);
			msg.addNumber(yinpiao);
			player.sendPacket(msg);
		}

	}

	public int getMoneySliver() {
		return _moneySliver.get();
	}

	public int getMoneyNote() {
		return _moneyNote.get();
	}

	public int getMoneyYuanBao() {
		return _moneyYuanBao.get();
	}

	public LinkedList<SalesRecord> getSalesRecords() {
		return buySellRecords;
	}

	public void addRecord(SalesRecord re) {
		if (getSalesRecords().size() >= RECORDLIMIT) {
			getSalesRecords().removeLast();
		}
		buySellRecords.addFirst(re);
	}

	public SalesRecord createSalesRecord(final String itemName, final int count, final int money, final int _moneyType) {
		return new SalesRecord(itemName, count, money, _moneyType);
	}

	public static class SalesRecord {
		private String time;

		private String itemName;

		private int count;

		private int money;

		private String moneyType;

		public SalesRecord(final String itemName, final int count, final int money, final int _moneyType) {
			super();
			time = "" + Calendar.getInstance().get(Calendar.MONTH + 1) + "-" + Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
			this.itemName = itemName;
			this.count = count;
			this.money = money;
			if (_moneyType == PcInventory.MONEY_NOTE_ID) {
				this.moneyType = "银票";
			}
			if (_moneyType == PcInventory.MONEY_YAUNBAO_ID) {
				this.moneyType = "元宝";
			}
		}

		public String getTime() {
			return time;
		}

		public String getItemName() {
			return itemName;
		}

		public int getCount() {
			return count;
		}

		public int getMoney() {
			return money;
		}

		public String getMoneyType() {
			return moneyType;
		}

	}
}
